// 云函数入口文件
const cloud = require('wx-server-sdk')
const env = 'bbq-6gz7knah1c296b0f'
cloud.init(env)

const db = cloud.database({env})
// 云函数入口函数
exports.main = async (event) => {
  const OPEN_ID = cloud.getWXContext().OPENID

  const result = await db.collection('users').where({
    userId:OPEN_ID
  }).get()

  if(result.data.length){    //查询不到openID
    return {
      code:403,
      msg:'账号已注册'
    }
  }
  const res = await db.collection('users').add({
       data:{
        userId:OPEN_ID,
        userName:'一只小可爱',
        account:event.account,
        password:event.password,
        gender:0,
        avatarUrl:'https://ts1.cn.mm.bing.net/th/id/R-C.e7fee7d5008df9fdf859e53dde610a03?rik=RuXsj0R%2fFZZMYQ&riu=http%3a%2f%2fpic.22520.cn%2fup%2f200709%2f1594309644607694.jpg&ehk=SOzzuncoMx9a5Ivw5EpiTZp48w%2f0i1e19aSLwylFaV4%3d&risl=&pid=ImgRaw&r=0',
        bannedTime:new Date().getTime(), //封号时间
        silenceTime:new Date().getTime(),  //禁言时间
        stores:[],
        likes:[],
        fans:[]
       }
  })
  return {
    msg:'注册成功'
  }

}